package com.java.test.system.fundstypemanage;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.java.test.dto.PageResultDTO;
import com.java.test.dto.ResponseDTO;
import com.java.test.system.feesettings.entity.FundsTypeEntity;
import com.java.test.system.fundstypemanage.constant.FundsTypeResponseCodeConst;
import com.java.test.system.fundstypemanage.dto.CalculationAmountDTO;
import com.java.test.system.fundstypemanage.dto.FundsTypeAddDTO;
import com.java.test.system.fundstypemanage.dto.FundsTypeQueryDTO;
import com.java.test.system.fundstypemanage.dto.FundsTypeUpdateDTO;
import com.java.test.system.fundstypemanage.vo.FundsTypeVO;
import com.java.test.timer.MessagePushTimer;
import com.java.test.util.RequestGameServiceUtil;
import com.java.test.util.SmartBeanUtil;
import com.java.test.util.SmartPageUtil;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.Timer;

@Service
public class FundsTypeService {

    @Autowired
    private FundsTypeDAO fundsTypeDAO;
    @Value("${get.game.offLineUser.url}")
    private String offLineUserUrl;

    private static Timer timer=new Timer();

    /**
     * 获取数据
     * @param queryDTO
     * @return
     */
    public ResponseDTO<PageResultDTO<FundsTypeVO>> getFundsTypeList(FundsTypeQueryDTO queryDTO){
        Page page = SmartPageUtil.convert2QueryPage(queryDTO);
        List<FundsTypeEntity> entityList = fundsTypeDAO.getFundsTypeList(queryDTO);
        PageResultDTO<FundsTypeVO> pageResultDTO = SmartPageUtil.convert2PageResult(page, entityList, FundsTypeVO.class);
        return ResponseDTO.succData(pageResultDTO);
    }

    /**
     * 保存
     * @param addDTO
     * @author 
     * @date 2020/7/15 0015 13:53 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> save(FundsTypeAddDTO addDTO) {
        if("1".equals(addDTO.getIsDefault())){
            fundsTypeDAO.updateByIsDefault();
        }
        FundsTypeEntity entity = fundsTypeDAO.getByGName(addDTO);
        if (entity != null) {
            return ResponseDTO.wrap(FundsTypeResponseCodeConst.ALREADY_EXIST);
        }
        int i = fundsTypeDAO.insertSave(addDTO);
        if(i>0){
            MessagePushTimer.fundsCLQ.offer("fundsTypePush");
        }
        return ResponseDTO.succ();
    }

    /**
     * 查询数据ById
     * @param id
     * @author 
     * @date 2020/7/15 0015 13:53 
     * @return com.java.test.dto.ResponseDTO<com.java.test.system.fundstypemanage.vo.FundsTypeVO>
     */
    public ResponseDTO<FundsTypeVO> selectById(String id) {
        String areaName = " ";
        FundsTypeEntity entity = fundsTypeDAO.getById(id);
        entity.setFullName(areaName.substring(0,areaName.length()-1));
        FundsTypeVO configDTO = SmartBeanUtil.copy(entity, FundsTypeVO.class);
        return ResponseDTO.succData(configDTO);
    }

    /**
     * 更新
     * @param updateDTO
     * @author 
     * @date 2020/7/15 0015 13:53 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> updateById(FundsTypeUpdateDTO updateDTO) {
        if("1".equals(updateDTO.getIsDefault())){
            fundsTypeDAO.updateByIsDefault();
        }
        int i = fundsTypeDAO.updateById(updateDTO);
        if(i>0){
            MessagePushTimer.fundsCLQ.offer("fundsTypePush");
        }
        return ResponseDTO.succ();
    }

    /**
     *删除
     * @author 
     * @date 2020/7/15 0015 13:53 
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> deleteById(String id) {
        FundsTypeEntity entity = fundsTypeDAO.getById(id);
        if (entity == null) {
            return ResponseDTO.wrap(FundsTypeResponseCodeConst.NOT_EXIST);
        }
        fundsTypeDAO.deleteById(id);
        return ResponseDTO.succ();
    }
    
    /**
     * 推送
     * @author 
     * @date 2020/7/15 0015 13:54
     * @return com.java.test.dto.ResponseDTO<java.lang.String>
     */
    public ResponseDTO<String> messagePush() {
        List<FundsTypeEntity> entityList = fundsTypeDAO.getFundsTypeList(new FundsTypeQueryDTO());
        String time = new Date().getTime()+"";
        StringBuffer stra = new StringBuffer("[");
        for (FundsTypeEntity entity:entityList) {
            StringBuffer str = new StringBuffer("[\"");
            str.append(entity.getFullName()).append("\",\"");
            str.append(entity.getName()).append("\",");
            str.append(entity.getRate()).append(",");
            str.append(entity.getIsDefault()).append("],");
            stra.append(str);
        }
        String data = "{\"code\":6,\"data\":"+stra.toString().substring(0,stra.length()-1)+"]}";
        Random r1 = new Random();
        String url =  RequestGameServiceUtil.createUrl(time,r1.nextInt(100)+"",data,offLineUserUrl);
        JSONObject resMsg = RequestGameServiceUtil.getService(url);
        String state = (resMsg.get("state").toString());
        if("0".equals(state)){
            return ResponseDTO.succ();
        }else {
            return ResponseDTO.error(resMsg.get("message").toString());
        }
    }

    /**
     * 计算当前资金类型对应基础资金类型金额
     * @author 
     * @date 2020/10/15 0015 9:54 
     * @return double
     */
    public double calculationAmount(CalculationAmountDTO dto){
        List<FundsTypeEntity> entityList = fundsTypeDAO.getFundsTypeList(dto);
        for (FundsTypeEntity obj:entityList) {
            if(obj.getName().equals(dto.getFoundsType())){
                return obj.getRate().doubleValue()*dto.getAmount();
            }
        }
        return 0.0;
    }
}
